package com.zju.cst.cloudApp.dao.impl;

import java.util.Map;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.defaults.DefaultSqlSessionFactory;

import com.zju.cst.cloudApp.dao.Dao;

public class DaoImpl<T> implements Dao<T> {

	protected SqlSession session;
	private DefaultSqlSessionFactory sqlSessionFactory;
	
	private String prefix;
	private Class<T> clazz;
	
	public void openSession() {
		session = sqlSessionFactory.openSession(true);
	}
	
	@Override
	public T[] select(Map<String, String> map) {
		if(session == null) {
			openSession();
		}
		return null;
	}

	@Override
	public int update(T t) {
		if(session == null) {
			openSession();
		}
		int result = session.update((new StringBuilder(prefix).append(".update").toString()), t);
		return result;
	}

	@Override
	public int add(T t) {
		if(session == null) {
			openSession();
		}
		int result = session.insert((new StringBuilder(prefix).append(".insert").toString()), t);
		return result;
	}

	@Override
	public int deleteById(String id) {
		if(session == null) {
			openSession();
		}
		return 0;
	}

	public DefaultSqlSessionFactory getSqlSessionFactory() {
		return sqlSessionFactory;
	}

	public void setSqlSessionFactory(DefaultSqlSessionFactory sqlSessionFactory) {
		this.sqlSessionFactory = sqlSessionFactory;
	}

	public String getPrefix() {
		return prefix;
	}

	public void setPrefix(String prefix) {
		this.prefix = prefix;
	}

	public Class<T> getClazz() {
		return clazz;
	}

	public void setClazz(Class<T> clazz) {
		this.clazz = clazz;
	}

}
